<?php 
    require_once("include/game_core.php");   
    require_once(USER_CONTROLLER_FILE);

    if ( (! UserController::isLoggedIn()) ||
            (! UserController::isPlaying())  ) {
        header( 'Location: index.php' ) ;
    }
    
    require_once(VIEW_BASE_FILE);
    require_once(GAME_CONTROLLER_FILE);
    require_once (GAME_TABLE_FILE);
    
    $view = new view_base();
    $view->ShowHeader();
    $view->ShowMenu("none");
    $view->ShowConatinerHead();
    

    $game_id = GameController::getLastGameId();
    $uid = UserController::getUserId();
    $result =  GameTable::getOldGameRow($game_id) ; 
    
    if ( ($result == null) && (! DEBUG) ) {
        header( 'Location: index.php' ) ;
    }
    
?>
<div id="content">
<?php
    if($result == null) {
        //$_ERROR = "somthing worng with db";
        if (DEBUG) {
            echo 'Something Wrong with getting row from Old Games table where game id=' . $game_id;
        }
        //UserController::SetPlaying(0);
        include (VIEW_SIDE_FILE);
        $view->ShowFooter();
        die();
    }

    //$_GAME_ID = $result['game_id'];
    $_DURATION = $result['duration'];
    $_WON = $result['won'];
    $_LOST = $result['lost'];
    $_MOVES = $result['moves'];
    $_WON_DMG = $result['won_dmg'];
    $_LOSE_DMG = $result['loose_dmg'];
    
    $_TITLE = "";
    $_IMG_TITLE = "";
    $_MyDMG = 0;
    $_OpDMG = 0;
    
    $_Winner_Avatar = 0;
    $_Winner_Nick = 0;
    
    if($_WON == $uid) { // if we won
        $_TITLE = "You Won !";
        $_IMG_TITLE = "win";
        $_MyDMG = $_WON_DMG;
        $_OpDMG = $_LOSE_DMG;
        $_Winner_Avatar = UserController::getUserAvatar();
        $_Winner_Nick = UserController::getUserNickname();
        UserController::setUserWinCount(UserController::getUserWinCount()+1);   //update user info
    }
    else {
        $_TITLE = 'You Lost.';   
        $_IMG_TITLE = "loose";
        $_MyDMG = $_LOSE_DMG;
        $_OpDMG = $_WON_DMG;     
        $winner_info = GameTable::getOpponentNickAndAvatarFromOldGameTable();
        $_Winner_Avatar = $winner_info[1];
        $_Winner_Nick = $winner_info[0];   
        UserController::setUserLooseCount(UserController::getUserLooseCount()+1);   //update user info
    }
    
    //update user information:
    UserController::setUserPoints(UserController::getUserPoints() + $_MyDMG);    
    
    
    //update user scores, according to $uidin DB
    $query = "UPDATE users SET points = (points+'$_MyDMG')  WHERE id=$uid";

    $db = new DBConnection();
    if ($db->executeQuery($query)) {
        $result = $db->getResult();
        if( ($result != null) ) {
            // OK
        }
        if (DEBUG) {
            echo 'DB ERROR';
        }
    }
    else {
        if (DEBUG) {
            echo 'DB ERROR';
        }
   }
    
            
                
?>

    <table width="100%">
        <tr>
            <td><h2><?php echo $_TITLE; ?></h2></td><td width="70"><img src="images/game/<?php echo $_IMG_TITLE; ?>.png"</td>
        </tr>
    </table>
    <h3>Game Statistics</h3>
    <br/>
    <table width="100%">
        <tr>
            <td>Winner</td>
            <td>
                <table>
                    <tr>
                        <td><img src="images/avatars/<?php echo $_Winner_Avatar;  ?>.png" height="30" width="30"></td>
                        <td><?php  echo $_Winner_Nick ; ?></td>
                    </tr>
                </table>                
            </td>
        </tr>
        <tr>
            <td>Total game time</td>
            <td><?php  echo $_DURATION ; ?></td>
        </tr>
        <tr>
            <td>Moves</td>
            <td><?php  echo $_MOVES ; ?></td>
        </tr>   
        <tr>
            <td>Your attack damage</td>
            <td><?php  echo $_MyDMG ; ?></td>
        </tr>  
        <tr>
            <td>Opponent attack damage</td>
            <td><?php  echo $_OpDMG ; ?></td>
        </tr>          
    </table>
    
            
</div>

<?php 
    //clear currenet game stats
    UserController::SetPlaying(0);
    include (VIEW_SIDE_FILE); 
    $view->ShowFooter();      
?>